package com.mzy.linear_struct.subject;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: codermzy
 * @Date: 2024/04/18/11:52
 * @Description:
 */
public class 罗马数字转整数13 {


    Map<Character, Integer> map = new HashMap<Character, Integer>() {{
        put('I', 1);
        put('V', 5);
        put('X', 10);
        put('L', 50);
        put('C', 100);
        put('D', 500);
        put('M', 1000);
    }};

    public int romanToInt(String s) {
        int answer = 0;
        for (int i = 0; i < s.length(); i++) {
            int value = map.get(s.charAt(i));

            if (i + 1 < s.length() && value < map.get(s.charAt(i + 1))) {
                answer -= value;
            } else {
                answer += value;
            }

        }

        return answer;
    }

    public static void main(String[] args) {
        System.out.println(new 罗马数字转整数13().romanToInt("LVIII"));
    }
}
